package com.yanqu.road.server.manger.union.listener;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.dafuweng.DFWConfig;
import com.yanqu.road.entity.activity.hanghai.config.HangHaiConfig;
import com.yanqu.road.entity.activity.seacraft.pirateriot.config.SeacraftPirateRiotConfig;
import com.yanqu.road.entity.enums.activity.eActivityCookBoyType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eUnionPosition;
import com.yanqu.road.entity.enums.eUnionSyncType;
import com.yanqu.road.entity.task.args.RelationActivityUnionRiseRankArgs;
import com.yanqu.road.entity.task.args.UnionRiseRankArgs;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.module.activity.tomb.TombMgr;
import com.yanqu.road.server.manger.ICrossUnionSyncListener;
import com.yanqu.road.server.manger.activity.*;
import com.yanqu.road.server.manger.activity.bowart.BowArtMgr;
import com.yanqu.road.server.manger.activity.dafuweng.DaFuWengMgr;
import com.yanqu.road.server.manger.activity.dragonboatrace.DragonBoatRaceMgr;
import com.yanqu.road.server.manger.activity.ghost.GhostMgr;
import com.yanqu.road.server.manger.activity.hanghai.HangHaiMgr;
import com.yanqu.road.server.manger.activity.orewar.OreWarActivityMgr;
import com.yanqu.road.server.manger.activity.orewar.OreWarActivityRankMgr;
import com.yanqu.road.server.manger.activity.peakseacraft.PeakSeacraftMgr;
import com.yanqu.road.server.manger.activity.seacraft.SeacraftPirateRiotMgr;
import com.yanqu.road.server.manger.activity.seacraft.SeacraftSouthSeaMgr;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdActivityMgr;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdActivityRankMgr;
import com.yanqu.road.server.manger.activity.twins.TwinsMgr;
import com.yanqu.road.server.manger.activity.unionwar.UnionPhaseWarMgr;
import com.yanqu.road.server.manger.beautyescort.BeautyEscortMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.manger.union.UnionActivityMgr;

import java.math.BigInteger;
import java.util.List;

public class UnionMemberPositionListener implements ICrossUnionSyncListener {


    @Override
    public void notifySync(int syncType, long playerId, CrossUnionInfo unionInfo, List<CrossUnionMember> memberList) {

        if(syncType == eUnionSyncType.SetPosition.getValue()){

            CrossUnionMember operateMember = memberList.get(0);
            if(operateMember.getServerId() == GameServer.getInstance().getServerId()){
                notifyUnionActivityMasterChange(operateMember.getUserId(), unionInfo.getUnionUid());

                UnionPhaseWarMgr.memberPositionChange(unionInfo, operateMember.getUserId(), operateMember.getPosition());
                BeautyEscortMgr.memberPositionChange(unionInfo, operateMember.getUserId(), operateMember.getPosition());
            }

            //如果是转让会长
            if(operateMember.getPosition() == eUnionPosition.Master.getValue()){
                CrossUnionMember roleMember = CrossUnionMgr.getUnionMember(unionInfo.getUnionUid(), playerId);
                if(roleMember != null && roleMember.getServerId() == GameServer.getInstance().getServerId()){
                    notifyUnionActivityMasterChange(playerId, unionInfo.getUnionUid());

                    UnionPhaseWarMgr.memberPositionChange(unionInfo, playerId, eUnionPosition.Member.getValue());
                    BeautyEscortMgr.memberPositionChange(unionInfo, playerId, eUnionPosition.Member.getValue());
                    SeacraftSouthSeaMgr.synUnionInfo(1, unionInfo.getUnionUid(), 0, unionInfo);
                }
            }
        }

    }


    private static void notifyUnionActivityMasterChange(long userId, String unionUid) {
        notifyCommonActivityMasterChange(unionUid);
        List<ActivityInfo> activityInfoList = NormalActivityMgr.getUnionRiseRankActivityList();
        for (ActivityInfo activityInfo : activityInfoList) {
            UnionActivityMgr.changeUnionActivityValue(activityInfo.getActivityId(), unionUid, 0, null, UnionRiseRankArgs.NOTIFY_TYPE_POSITION_CHANGE);
        }
        if (!StrongholdActivityMgr.isGameFinish()) {
            ActivityInfo strongholdActivityInfo = StrongholdActivityMgr.getOpenActivityInfo();
            if (strongholdActivityInfo != null) {
                StrongholdActivityRankMgr.changeUnionActivityValue(strongholdActivityInfo.getActivityId(), StrongholdActivityMgr.CONDITION_TYPE_WILD_UNION, unionUid, userId, null, UnionRiseRankArgs.NOTIFY_TYPE_NORMAL);
                StrongholdActivityRankMgr.changeUnionActivityValueNotUpload(strongholdActivityInfo.getActivityId(), StrongholdActivityMgr.CONDITION_TYPE_ELIMINATE_UNION, unionUid, userId, null, UnionRiseRankArgs.NOTIFY_TYPE_NORMAL);
            }
        }

        //盐场
        List<ActivityInfo> oreActivityInfoList = OreWarActivityMgr.getOpenActivityInfoList();
        for (ActivityInfo activityInfo : oreActivityInfoList) {
            if (ActivityMgr.activityInTime(activityInfo)) {
                OreWarActivityRankMgr.changeUnionActivityValue(activityInfo.getActivityId(), unionUid, userId, null, UnionRiseRankArgs.NOTIFY_TYPE_POSITION_CHANGE);
            }
        }

        //弓术
        ActivityInfo bowArtActivityInfo = BowArtMgr.getActivityInfo();
        if(ActivityMgr.activityInTime(bowArtActivityInfo)){
            int type = BowArtMgr.CONDITION_TYPE_SERVER_UNION;
            List<ActivityConditionInfo> list = NormalActivityMgr.getActivityConditionInfoList(bowArtActivityInfo.getActivityId());
            if(list != null) {
                for (ActivityConditionInfo activityConditionInfo : list) {
                    if (activityConditionInfo.getType() == BowArtMgr.CONDITION_TYPE_CROSS_UNION) {
                        type = BowArtMgr.CONDITION_TYPE_CROSS_UNION;
                        break;
                    }
                }
                GeneralActivityRankMgr.changeUnionActivityValue(bowArtActivityInfo.getActivityId(), type, unionUid, userId, null, UnionRiseRankArgs.NOTIFY_TYPE_POSITION_CHANGE);
            }
        }

        // 大富翁
        DFWConfig dfwConfig = DaFuWengMgr.getDfwConfig();
        if (dfwConfig != null && ActivityMgr.activityInTime(dfwConfig.getActivityInfo())) {
            for (int type : NormalActivityMgr.getActivityConditionUnionRankType(dfwConfig.getActivityInfo().getActivityId())) {
                GeneralActivityRankMgr.changeUnionActivityValue(dfwConfig.getActivityInfo().getActivityId(), type, unionUid, userId, null, UnionRiseRankArgs.NOTIFY_TYPE_POSITION_CHANGE);
            }
        }
        // 鬼市
        ActivityInfo ghostInfo = GhostMgr.getActivityInfo();
        if (ActivityMgr.activityInTime(ghostInfo)) {
            for (int type : NormalActivityMgr.getActivityConditionUnionRankType(ghostInfo.getActivityId())) {
                GeneralActivityRankMgr.changeUnionActivityValue(ghostInfo.getActivityId(), type, unionUid, userId, null, UnionRiseRankArgs.NOTIFY_TYPE_POSITION_CHANGE);
            }
        }
        // 大航海
        HangHaiConfig hangHaiConfig = HangHaiMgr.getHangHaiConfig();
        if (hangHaiConfig != null && ActivityMgr.activityInTime(hangHaiConfig.getActivityInfo())) {
            for (int type : NormalActivityMgr.getActivityConditionUnionRankType(hangHaiConfig.getActivityId())) {
                GeneralActivityRankMgr.changeUnionActivityValue(hangHaiConfig.getActivityId(), type, unionUid, userId, null, UnionRiseRankArgs.NOTIFY_TYPE_POSITION_CHANGE);
            }
        }
        // 赛龙舟
        ActivityInfo dragonBoatRaceInfo = DragonBoatRaceMgr.getActivityInfo();
        if (ActivityMgr.activityInTime(dragonBoatRaceInfo)) {
            for (int type : NormalActivityMgr.getActivityConditionUnionRankType(dragonBoatRaceInfo.getActivityId())) {
                GeneralActivityRankMgr.changeUnionActivityValue(dragonBoatRaceInfo.getActivityId(), type, unionUid, userId, null, UnionRiseRankArgs.NOTIFY_TYPE_POSITION_CHANGE);
            }
        }
        // 海盗之乱
        SeacraftPirateRiotConfig config = SeacraftPirateRiotMgr.getPirateRiotConfig();
        if (SeacraftPirateRiotMgr.inUnionRankChangeTime(config)) {
            GeneralActivityRankMgr.changeUnionActivityValue(config.getActivityInfo().getActivityId(), eGamePlayerEventType.SeacraftPirateRiotUnionScoreRank.getValue(), unionUid, userId, null, UnionRiseRankArgs.NOTIFY_TYPE_POSITION_CHANGE);
        }

        // 秦始皇陵
        ActivityInfo tombActivityInfo = TombMgr.getActivityInfo();
        if (ActivityMgr.activityInTime(tombActivityInfo)){
            for (int type : NormalActivityMgr.getActivityConditionUnionRankType(tombActivityInfo.getActivityId())){
                GeneralActivityRankMgr.changeUnionActivityValue(tombActivityInfo.getActivityId(), type, unionUid, userId, null, UnionRiseRankArgs.NOTIFY_TYPE_POSITION_CHANGE);
            }
        }

        //绝代
        ActivityInfo twinsActivityInfo = TwinsMgr.getActivity();
        if (ActivityMgr.activityInTime(twinsActivityInfo)){
            for (int type : NormalActivityMgr.getActivityConditionUnionRankType(twinsActivityInfo.getActivityId())){
                GeneralActivityRankMgr.changeUnionActivityValue(twinsActivityInfo.getActivityId(), type, unionUid, userId, null, UnionRiseRankArgs.NOTIFY_TYPE_POSITION_CHANGE);
            }
        }


        List<ActivityInfo> relationActivityInfoList = RelationActivityMgr.getOpenActivityInfoList();
        for (ActivityInfo activityInfo : relationActivityInfoList) {
            if (activityInfo.getType() == eActivityType.CookingBoyActivity.getValue() && activityInfo.getChildType() == eActivityCookBoyType.Process1.getChildType()) {
                RelationActivityMgr.conditionNotify(activityInfo.getActivityId(),userId, eGamePlayerEventType.CookBoy1IntegralUserCrossRank.getValue(),
                        new RelationActivityUnionRiseRankArgs(UnionRiseRankArgs.NOTIFY_TYPE_REMOVE, BigInteger.ZERO, unionUid, activityInfo.getActivityId()));
            }
        }

    }

    private static void notifyCommonActivityMasterChange(String unionUid) {
        List<ActivityInfo> activityInfoList = CommonActivityMgr.getInTimeCommonActivityInfoList(eActivityType.CommonActivity.getValue());
        for (ActivityInfo activityInfo : activityInfoList) {
            CommonActivityRankMgr.changeUnionActivityValue(activityInfo.getActivityId(), unionUid, 0, null, UnionRiseRankArgs.NOTIFY_TYPE_NORMAL);
        }
    }

}
